#include "Rp2Texture.h"
#include "Rp2Catalog.h"

using namespace Rp2;

RP2_IMPLEMENT_RTTI(Rp2, Texture, Object);
RP2_IMPLEMENT_DEFAULT_NAME_ID(Texture, Object);

const char* Texture::ms_aacFilterType[MAX_FILTER_TYPES] =
{
    "NEAREST",
    "LINEAR",
    "NEAREST_NEAREST",
    "NEAREST_LINEAR",
    "LINEAR_NEAREST",
    "LINEAR_LINEAR"
};

const char* Texture::ms_aacWrapType[MAX_WRAP_TYPES] =
{
    "CLAMP",
    "REPEAT",
    "MIRRORED_REPEAT",
    "CLAMP_BORDER",
    "CLAMP_EDGE"
};

//---------------------------------------------------------------------------------------------------
Texture::Texture (const char* acTextureName, Image* pkImage)
    :
    m_spkImage(pkImage),
    m_kBorderColor(ColorRGBA::BLACK)
{
    if (acTextureName)
    {
        SetName(acTextureName);
    }
    else if (pkImage)
    {
        SetName(pkImage->GetName());
    }
    else
    {
        SetName("");
    }

    m_eFType = LINEAR;
    m_aeWType[0] = CLAMP_EDGE;
    m_aeWType[1] = CLAMP_EDGE;
    m_aeWType[2] = CLAMP_EDGE;
    m_fAnisotropy = 1.0f;
    m_bOffscreenTexture = false;

    Catalog<Texture>::GetActive()->Insert(this);
}
//---------------------------------------------------------------------------------------------------
Texture::Texture()
    :
    m_spkImage(0),
    m_kBorderColor(ColorRGBA::BLACK)
{
    SetName("");
    m_eFType = LINEAR;
    m_aeWType[0] = CLAMP_EDGE;
    m_aeWType[1] = CLAMP_EDGE;
    m_aeWType[2] = CLAMP_EDGE;
    m_fAnisotropy = 1.0f;
    m_bOffscreenTexture = false;
}
//---------------------------------------------------------------------------------------------------
Texture::~Texture ()
{
    // Inform all renderers using this texture that it is being destroyed.
    // This allows the renderer to free up any associated resources.
    Release();

    Catalog<Texture>::RemoveAll(this);
}
//---------------------------------------------------------------------------------------------------
Texture* Texture::Load (const std::string &rkTextureName)
{

    Image* pkImage = Catalog<Image>::GetActive()->Find(rkTextureName);
    if (pkImage)
    {
        return new Texture(rkTextureName.c_str(), pkImage);
    }

    return 0;
}
//---------------------------------------------------------------------------------------------------